package com.floreantpos.bo.ui.explorer;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.floreantpos.POSConstants;
import com.floreantpos.main.Application;
import com.floreantpos.model.dao.StoreDAO;
import com.floreantpos.swing.MessageDialog;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.PosTableRenderer;
import com.floreantpos.ui.dialog.POSMessageDialog;

public class StoreForwardExplorer extends TransparentPanel {
	private List dataList;

	private JTable table;

	private StoreRestExplorerTableModel tableModel;

	private StoreDAO dao;

	public StoreForwardExplorer() {
		this(POSConstants.ADMIN);
	}

	public StoreForwardExplorer(String role) {
		this.setName(POSConstants.STORE_FORWARD);
		Application.getInstance().setLastCommand(role);

		dao = new StoreDAO();
		dataList = dao.findAllStoreByToday();

		tableModel = new StoreRestExplorerTableModel();
		table = new JTable(tableModel);
		table.setDefaultRenderer(Object.class, new PosTableRenderer());

		setLayout(new BorderLayout(5, 5));
		add(new JScrollPane(table));

		JButton addButton = new JButton(POSConstants.STORE_FORWARD_NEW);
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = table.getSelectedRow();

					if (index > -1) {
						Object[] o = (Object[]) dataList.get(index);

						if ((Integer) o[6] > 0) {

							int option = JOptionPane.showOptionDialog(
									Application.getInstance().getManagementWindow(
											POSConstants.STORE_FORWARD),
									POSConstants.STORE_FORWARD_CONFIRM,
									POSConstants.CONFIRM, JOptionPane.YES_NO_OPTION,
									JOptionPane.QUESTION_MESSAGE, null, null, null);
							if (option != JOptionPane.YES_OPTION) {
								return;
							}

							dao.addStoreForward((String) o[1], (Integer) o[6],
									Application.getCurrentUser(),
									(Integer) o[0], "today");
							table.clearSelection();
							tableModel.updateModel(index);
							table.updateUI();
						}else{
							MessageDialog.showError(POSConstants.STORE_FORWARD_ZERO_ERROR);
						}
					}
				} catch (Exception x) {
					x.printStackTrace();
					MessageDialog.showError(POSConstants.ERROR_MESSAGE, x);
				}
			}

		});

		TransparentPanel panel = new TransparentPanel();
		panel.add(addButton);
		add(panel, BorderLayout.SOUTH);
	}

	class StoreRestExplorerTableModel extends AbstractTableModel {
		String[] columnNames = { POSConstants.STORE_NAME,
				POSConstants.STORE_TODAY, POSConstants.STORE_LASTDAY,
				POSConstants.STORE, POSConstants.STORE_TOTALSALES,
				POSConstants.STORE_LOSS, POSConstants.STORE_REST,
				POSConstants.STORE_REST_REAL,
				POSConstants.STORE_FORWARD_CONFIRM_HEAD };

		public int getRowCount() {
			if (dataList == null) {
				return 0;
			}
			return dataList.size();
		}

		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public String getColumnName(int column) {
			return columnNames[column];
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			if (dataList == null)
				return "";

			Object[] data = (Object[]) dataList.get(rowIndex);

			for (int i = 0; i < data.length; i++) {
				if (data[i] == null)
					data[i] = 0;
			}

			switch (columnIndex) {
			case 0:
				return String.valueOf(data[1]);

			case 1:
				return String.valueOf(data[2]);

			case 2:
				return String.valueOf(data[3]);

			case 3:
				return String.valueOf((Integer) data[2] + (Integer) data[3]);

			case 4:
				return String.valueOf(data[4]);

			case 5:
				return String.valueOf(data[5]);

			case 6:
				return String.valueOf(((Integer) data[2] + (Integer) data[3])
						- ((Integer) data[4] + (Integer) data[5]));

			case 7:
				return String.valueOf(data[6]);

			case 8:
				return (String.valueOf(data[7]).equals("0") ? POSConstants.NO
						: POSConstants.YES);

			}
			return null;
		}

		public void updateModel(int row) {
			int size = dataList.size();
			Object[] o = (Object[]) dataList.get(row);
			o[7] = "1";
			fireTableRowsUpdated(size, size);
		}

	}
}
